<?php

namespace redis;


class MyRedis
{
    protected $options = [
        'host'       => '127.0.0.1',
        'port'       => 6379,
        'password'   => '',
        'select'     => 0,
        'timeout'    => 0,
        'expire'     => 1800000,
        'persistent' => false,
        'prefix'     => '',
    ];
    private static  $handler;

    private  function __construct()
    {

      
        if (extension_loaded('redis')) {
            $redis = new \Redis();
            $redis->connect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout']);

            if ('' != $this->options['password']) {
                $redis->auth($this->options['password']);
            }
        } else {
            throw new \BadFunctionCallException('not support: redis');
        }

        if (0 != $this->options['select']) {
            $redis->select((int) $this->options['select']);
        }

        self::$handler = $redis;
    }

    /**
     * 调用
     * @return MyRedis
     */
    public static function getInstance()
    {
        if (!self::$handler instanceof self){
            new self();
        }
        return self::$handler;
    }

    private function __clone()
    {

    }
    public static function delCache($pre_key = 'xxxx'){
        $redis = self::getInstance();
        $keys  = $redis->keys("$pre_key*");
        $redis->del($keys);
    }
    public static function pfAdd($list_key,$key ){
        $redis = self::getInstance();
        return $redis->pfAdd($list_key,[$key]);
    }

}